<template>
  <div class="home-page">
    <!-- 头部搜索 -->
    <van-nav-bar class="login-page-nav home-nav" fixed>
      <van-button class="search-box" slot="title" icon="search" type="info" round to="/search">搜索</van-button>
    </van-nav-bar>
    <!-- tab切换 -->
    <van-tabs v-model="active" animated swipeable class="home-tab">
      <van-tab
        v-for="item in userChannel"
        :key="item.id"
        :title="item.name"
        class="tab-item"
        :name="item.id"
      >
        <!-- 文章列表组件 -->
        <article-list :channel="item"></article-list>
      </van-tab>
      <!-- 汉堡按钮 -->
      <template #nav-right>
        <i class="iconfont icongengduo" @click="popup=true"></i>
        <div class="placeholder"></div>
      </template>
      <!-- 占位标签，不参与flex计算，帮助更多按钮不遮挡tab -->
    </van-tabs>
    <!-- 编辑频道弹出层 -->
    <van-popup
      v-model="popup"
      closeable
      close-icon-position="top-left"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <!-- 频道编辑组件,传递tab激活索引对应，传递频道列表 -->
      <channel-edit
        :channelList="userChannel"
        :active="active"
        @changeActive="changeActive($event)"
        @changeChannels="changeChannels"
      ></channel-edit>
    </van-popup>
  </div>
</template>

<script>
import { getChannel } from '@/api/user.js'
import ArticleList from './components/article.vue'
import channelEdit from '@/components/articleItems/channelEdit'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'
export default {
  data() {
    return {
      active: 0,
      userChannel: [],
      popup: false
    }
  },

  components: { ArticleList, channelEdit },
  created() {
    this.getUserChannel()
  },
  methods: {
    async getUserChannel() {
      // 根据是否登录获取频道列表 后台会根据请求头中的token获取用户id对应的频道
      if (this.userToken) {
        try {
          const { data: res } = await getChannel()
          this.userChannel = res.data.channels
        } catch (err) {
          console.log(err)
          this.$toast.fail('获取用户频道失败')
        }
      } else {
        // 否则从本地拿数据
        this.userChannel = getItem('unlogin-channels')
        if (!this.userChannel) {
          // 没有本地频道数据，则请求获取默认推荐的频道列表
          try {
            const { data: res } = await getChannel()
            this.userChannel = res.data.channels
          } catch (err) {
            console.log(err)
            this.$toast.fail('获取用户频道失败')
          }
        }
      }
    },
    changeActive(id) {
      // 子传父自定义事件改变active值，实现切换频道
      this.active = id
      // 通过传参确定是否处于完成编辑要关闭弹层
      this.popup = false
    },
    changeChannels(item) {
      // 子传父添加频道
      this.userChannel.push(item)
    }
  },
  computed: {
    ...mapState(['userToken'])
  }
}
</script>

<style lang='less' scoped>
.home-page {
  padding-top: 180px;
  /deep/ .home-nav {
    .van-nav-bar__title {
      max-width: unset !important;
      .search-box {
        background-color: rgba(255, 255, 255, 0.1);
        width: 555px;
        height: 64px;
        border: none;
        .van-button__icon {
          font-size: 32px;
          color: #fff;
        }
        .van-button__text {
          font-size: 28px;
        }
      }
    }
  }

  /deep/ .home-tab {
    height: 82px;
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      left: 0;
      right: 0;
      z-index: 1;
    }
    div.van-tabs__line {
      width: 31px !important;
      height: 6px;
      margin-bottom: 10px;
      background-color: yellowgreen;
    }
    .van-tab {
      min-width: 200px;
      border-right: 1px solid #edeff3;
      font-size: 30px;
      color: #777;
    }
    .van-tab--active {
      color: rgb(22, 76, 22);
    }
    .icongengduo {
      position: fixed;
      right: 0;
      width: 65px;
      height: 88px;
      font-size: 40px;
      background-color: #fff;
      opacity: 0.8;
      display: flex;
      align-items: center;
      justify-content: center;
      color: black;
      &::after {
        content: '';
        position: absolute;
        left: 0;
        width: 1px;
        height: 90%;
        background: url(../../assets/gradient-gray-line.png) no-repeat;
        background-size: contain;
      }
    }
    .placeholder {
      flex-shrink: 0;
      width: 65px;
    }
  }
}
</style>
